package com.itheima.web.controller.cargo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.itheima.domain.cargo.ExtCproduct;
import com.itheima.domain.cargo.ExtCproductExample;
import com.itheima.domain.cargo.Factory;
import com.itheima.domain.cargo.FactoryExample;
import com.itheima.service.cargo.ExtCproductService;
import com.itheima.service.cargo.FactoryService;
import com.itheima.util.UploadUtil;
import com.itheima.web.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping(value = "/cargo/extCproduct")
public class ExtCproductController extends BaseController {

    @Reference
    private ExtCproductService extCproductService;

    @Reference
    private FactoryService factoryService;

    @RequestMapping(value = "/list", name = "分页查询附件列表")
    public String list(@RequestParam(defaultValue = "1") int page,
                       @RequestParam(defaultValue = "5") int size,
                       String contractId, String contractProductId) {
        ExtCproductExample example = new ExtCproductExample();
        example.createCriteria().andContractProductIdEqualTo(contractProductId).andContractIdEqualTo(contractId).andCompanyIdEqualTo(companyId);
        PageInfo pageInfo = extCproductService.findAll(example, page, size);
        request.setAttribute("page", pageInfo);

        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList", factoryList);

        request.setAttribute("contractId", contractId);
        request.setAttribute("contractProductId", contractProductId);
        return "cargo/extc/extc-list";
    }

    @RequestMapping(value = "/edit")
    public String edit(ExtCproduct extCproduct, MultipartFile productPhoto) throws Exception{

        extCproduct.setCompanyId(companyId);
        extCproduct.setCompanyName(companyName);

        if (productPhoto!=null){
            String upload = new UploadUtil().upload(productPhoto.getBytes());
            extCproduct.setProductImage(upload);
        }

        if (StringUtil.isEmpty(extCproduct.getId())){
            //新建
            extCproduct.setId(UUID.randomUUID().toString());
            extCproductService.save(extCproduct);
        }else {
            //更新
            extCproductService.update(extCproduct);
        }

        return "redirect:/cargo/extCproduct/list.do?contractId="+extCproduct.getContractId()
                +"&contractProductId="+extCproduct.getContractProductId();
    }


    @RequestMapping(value = "/toUpdate")
    public String toUpdate(String id, String contractId, String contractProductId){

        ExtCproduct extCproduct = extCproductService.findById(id);
        request.setAttribute("extCproduct", extCproduct);

        request.setAttribute("contractId", contractId);
        request.setAttribute("contractProductId", contractProductId);

        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria factoryCriteria = factoryExample.createCriteria();
        factoryCriteria.andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList", factoryList);

        return "cargo/extc/extc-update";
    }

    @RequestMapping(value = "/delete")
    public String delete(String id, String contractId, String contractProductId){
        extCproductService.delete(id);
        return "redirect:/cargo/extCproduct/list.do?contractId="+contractId
                +"&contractProductId="+contractProductId;
    }
}
